#include <bits/stdc++.h>
using namespace std;

#define int long long

vector<vector<int>> multiply(vector<vector<int>> &mat, vector<vector<int>> &mat2) {
    vector<vector<int>> ret(mat.size(), vector<int>(mat2[0].size()));
    for (int i = 0; i < mat.size(); i++) {
        for (int j = 0; j < mat2[0].size(); j++) {
            int x = 0;
            for (int k = 0; k < mat[0].size(); k++) {
                x += mat[i][k] * mat2[k][j];
            }
            ret[i][j] = x;
        }
    }
    return ret;
}
// 矩阵快速幂
vector<vector<int>> matrixPower(vector<vector<int>> &mat, int p) {
    vector<vector<int>> ret(mat.size(), vector<int>(mat.size()));
    for (int i = 0; i < mat.size(); i++) {
        ret[i][i] = 1;
    }
    while (p) {
        if (p & 1) {
            ret = multiply(ret, mat);
        }
        p >>= 1;
        mat = multiply(mat, mat);
    }
    return ret;
} 

signed main() {
    int n;
    cin >> n;
    vector<vector<int>> mat(n, vector<int>(n)), mat2 = mat;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> mat[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> mat2[i][j];
        }
    }
    mat = matrixPower(mat, 3);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}